<?php

namespace App\Providers;

use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
        if ($this->app->environment() !== 'production') {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
        }
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // 非 local 环境统一转为 https
        $this->app['request']->server->set('HTTPS', !isLocal());
        // 添加手机号验证规则
        Validator::extend('phone', function ($attribute, $value, $parameters, $validator) {
            return preg_match("/^1[23456789]\d{9}$/", $value);
        });
        // 添加密码验证规则
        Validator::extend('pattern_password', function ($attribute, $value, $parameters, $validator) {
            return preg_match("/^(?=.*\d)(?=.*[a-zA-Z]).{8,16}$/", $value);
        });
        // 添加链接验证规则
        Validator::extend('pattern_domain', function ($attribute, $value, $parameters, $validator) {
            return preg_match("((\w+):\/\/([^/:]+)\/)", $value);
        });
        // 添加邮箱验证规则
        Validator::extend('pattern_email', function ($attribute, $value, $parameters, $validator) {
            return preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $value);
        });
    }
}
